package com.victualler.core.base.service.impl;

import com.victualler.core.base.dao.BaseDao;
import com.victualler.core.base.repository.IBaseQueryRepository;
import com.victualler.core.common.dto.PageDto;
import com.victualler.core.common.dto.QueryDto;
import com.victualler.core.base.repository.BaseRepository;
import com.victualler.core.base.service.BaseService;
import com.victualler.core.common.utils.SpecificationUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

/**
 * @Author: Guoshichang
 * @Date: Create in 18:04 2018/4/5
 * @Discription:    通用的实体类服务类  根据实体类型封装基本的CRUD方法（不含分页）
 */
public  abstract   class BaseServiceImpl<E extends BaseDao,R extends BaseRepository<E,Long>,QR extends IBaseQueryRepository<E,Long>>  implements BaseService<E,R,QR> {
    @Autowired
    protected   R  baseRepository;

    @Autowired
    protected  QR  baseQueryRepository;

    @Override
    public List<E> getAll() {
        return baseRepository.findAll();
    }

    @Override
    public E getById(Long id) {
        return baseRepository.findOne(id);
    }
    @Transactional
    @Override
    public void deleteById(Long id) {
        baseRepository.delete(id);
    }
    @Transactional
    @Override
    public void delete(E dao) {
        baseRepository.delete(dao);
    }
    @Transactional
    @Override
    public void save(E dao) {
        baseRepository.save(dao);
    }
    @Transactional
    @Override
    public void saveAll(List<E> daos) {
        baseRepository.save(daos);
    }

    @Override
    public long countAllPage() {
        long page = this.baseRepository.count()/10;
        if(this.baseRepository.count()%10>0){
            page +=1;
        }
        if(page == 0){
            page = 1;
        }
        return page;
    }

    /**
     * 按照页号进行取值
     * @param pageNo
     * @return
     */
    @Override
    public List<E> getAllByPage(int pageNo,boolean isAsc,String... sorts) {
        Sort.Direction sortDir ;
        if(isAsc){
            sortDir = Sort.Direction.ASC;
        }else{
            sortDir = Sort.Direction.DESC;
        }
        Pageable pageable = new PageRequest((pageNo-1) * 10, 10, sortDir, sorts);
        return this.baseRepository.findAll(pageable).getContent();
    }
    
    
    /**
     * 根据queryDtoList查询队列的查询条件 , 获取对应的分页结果
     * @param queryDtoList
     * @param pageNo
     * @return
     */
    public List<E> getByQueryList(List<QueryDto> queryDtoList,int pageNo){ 
    	Pageable pageable = new PageRequest((pageNo-1) * 10, 10); //根据页号获取对应的结果
//    	if(queryDtoList == null || queryDtoList.size() == 0 ){
//            return this.baseRepository.findAll(pageable).getContent();
//        }
        return this.baseRepository.findAll(SpecificationUtil.getSpecification(queryDtoList), pageable).getContent();
    }
    /**
     * 根据查询条件统计总页数
     * @param queryDtoList
     * @return
     */
    public long  countAllPage(List<QueryDto> queryDtoList){
    	  //根据查询条件统计对应的总条数 
    	  long page = this.baseRepository.count(SpecificationUtil.getSpecification(queryDtoList))/10;
          if(this.baseRepository.count()%10>0){
              page +=1;
          }
          if(page == 0){
              page = 1;
          }
          return page;
    }
    /**
     * 根据queryDtoList查询队列的查询条件 , 获取对应的所有结果
     * @param queryDtoList
     * @return
     */
    public List<E> getByQueryList(List<QueryDto> queryDtoList){
    	return this.baseRepository.findAll(SpecificationUtil.getSpecification(queryDtoList));
    }

    /**
     * @author Guoshichang
     * @since 14:58 on 2018/7/7.
     * @param requestDto
     * @Function  根据入参查询对象查询对应的页面
     */
    @Override
    public PageDto<E> getByRequestDto(Object requestDto, int pageNo, int limit) {
        return this.baseQueryRepository.queryEOPageByRequestDto(requestDto , pageNo , limit);
    }


}
